package com.zhanbo.algorithm;

import java.util.LinkedList;

/**
 * @author zhanbo
 * @version 1.0
 * @describe
 * @date 2020/7/29-17:59
 */
public class Solution9 {

    public static void main(String[] args) throws Exception {
        StackDemo stackDemo = new StackDemo();
        stackDemo.push(1);
        stackDemo.push(2);
        System.out.println(stackDemo.pop());
        System.out.println(stackDemo.pop());
        stackDemo.push(3);
        stackDemo.push(4);
        System.out.println(stackDemo.pop());
        stackDemo.push(5);

        System.out.println(stackDemo.pop());
    }




}

/**
 * 使用两个栈实现队列
 *
 */
class StackDemo{
    /**
     * 入栈--入队
     */
    LinkedList<Integer> in = new LinkedList<>();
    /**
     * 出栈--出队
     */
    LinkedList<Integer> out = new LinkedList<>();


    public void push(int node) {
        in.push(node);
    }

    public int pop() throws Exception {

        if (out.isEmpty()){
            while (!in.isEmpty()){
                out.push(in.pop());
            }
        }
        if (out.isEmpty()){
            throw new Exception("queue is empty");
        }


        return out.pop();
    }
}
